package com.own.component.store.redis.method;

import com.own.component.store.core.method.StoreValue;
import org.redisson.api.RedissonClient;

import java.time.Duration;

/**
 * DefaultRedisStoreValue
 *
 * @author chenxueli
 * @date 2022-06-15 03:14:15
 */
public class DefaultRedisStoreValue<T> extends DefaultBaseStoreMethod implements StoreValue<T> {

    public DefaultRedisStoreValue(RedissonClient redissonClient) {
        super(redissonClient);
    }

    /**
     * 设置缓存
     *
     * @param name  缓存KEY
     * @param value 缓存内容
     */
    @Override
    public void set(String name, T value) {
        getRedissonClient().getBucket(buildKey(name)).set(value);
    }

    /**
     * 设置缓存，指定过期时间
     *
     * @param name     缓存KEY
     * @param value    缓存内容
     * @param duration 指定缓存过期时间
     */
    @Override
    public void set(String name, T value, Duration duration) {
        getRedissonClient().getBucket(buildKey(name)).set(value, duration);
    }

    /**
     * 获取缓存
     *
     * @param name 缓存KEY
     * @return 缓存内容
     */
    @Override
    public T get(String name) {
        return getRedissonClient().<T>getBucket(buildKey(name)).get();
    }

}
